package com.jzo2o.foundations.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.foundations.entity.Serve;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 服务表 服务实现类        之所以进行严谨性判断，是为了防止请求是否是伪造的
 * </p>
 *
 * @author author
 * @since 2024-12-01
 */
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper,Serve> implements IServeService {

    /**
     *区域服务分页查询接口
     * @param reqDTO
     * @return
     */
    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO reqDTO) {
        return PageHelperUtils.selectPage(reqDTO,
                () -> baseMapper.finaServerListByRegionId(reqDTO.getRegionId()));
    }


    @Autowired
    private ServeItemMapper serveItemMapper;

    @Autowired
    private IRegionService regionService;


    /**
     * 添加区域服务
     * @param dtoList
     */
    @Override
    public void batchAdd(List<ServeUpsertReqDTO> dtoList) {
        for (ServeUpsertReqDTO dto : dtoList) {
            //判断服务项是否存在
            ServeItem serveItem = serveItemMapper.selectById(dto.getServeItemId());
            if (serveItem == null){
                throw new ForbiddenOperationException("服务项不存在");
            }
            //判断服务项是否启用
            if (serveItem.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()){
                throw new ForbiddenOperationException("服务项未启用");
            }
            //判断服务项是否已经添加过
            Integer count = this.lambdaQuery().eq(Serve::getRegionId, dto.getRegionId())
                    .eq(Serve::getServeItemId, dto.getServeItemId())
                    .count();
            if (count>0){
                throw new ForbiddenOperationException("服务项已经添加过了");
            }
            //构建服务项对象
            Serve serve = BeanUtils.copyBean(dto, Serve.class);
            Region region = regionService.getById(dto.getRegionId());
            serve.setCityCode(region.getCityCode());
            //添加服务项
            boolean save = this.save(serve);
            if (!save){
                throw new DBException("添加服务失败");
            }
        }
    }

    /**
     * 修改价格
     * @param id
     * @param price
     */
    @Override
    public void updatePrice(Long id, BigDecimal price) {
        //判断服务是否存在
        Serve serve = this.getById(id);
        if (serve == null){
            throw new ForbiddenOperationException("服务不存在");
        }
        //修改价格
        boolean update = this.lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getPrice, price)
                .update();
        if (!update){
            throw new DBException("更新价格失败");
        }
    }


    @Autowired
    private IServeService serveService;

    /**
     * 服务上架
     * @param id
     */
    @Override
    public void onSale(Long id) {
        Serve serve = serveService.getById(id);
        //判断服务是否存在
        if (serve == null){
            throw new ForbiddenOperationException("服务不存在");
        }
        //判断服务是否是上架状态
        if (serve.getSaleStatus() == FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务已上架");
        }
        //判断是否选择区域
        if (serve.getRegionId() == null){
            throw new ForbiddenOperationException("未选择区域");
        }
        ServeItem serveItem = serveItemMapper.selectById(serve.getServeItemId());
        //判断服务项是否存在
        if (serveItem == null){
            throw new ForbiddenOperationException("服务不存在");
        }
        //判断服务项是否启用
        if (serveItem.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务项未启用");
        }

        //修改状态为上架
        boolean result = this.lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus())
                .update();
        if (!result){
            throw new DBException("服务上架失败");
        }
    }


    /**
     * 服务下架
     * @param id
     */
    @Override
    public void offSale(Long id) {
        Serve serve = this.getById(id);
        //判断服务是否存在
        if (serve == null){
            throw new ForbiddenOperationException("服务不存在");
        }
        //判断服务是否是上架状态
        if (serve.getSaleStatus() != FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务未上架");
        }
        //判断是否选择区域
        if (serve.getRegionId() == null){
            throw new ForbiddenOperationException("未选择区域");
        }
        ServeItem serveItem = serveItemMapper.selectById(serve.getServeItemId());
        //判断服务项是否存在
        if (serveItem == null){
            throw new ForbiddenOperationException("服务项不存在");
        }
        //判断服务项是否启用
        if (serveItem.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务项未启用");
        }

        //修改状态为上架
        boolean result = this.lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus, FoundationStatusEnum.DISABLE.getStatus())
                .update();
        if (!result){
            throw new DBException("服务下架失败");
        }
    }

    /**
     * 删除区域服务
     * @param id
     */
    @Override
    public void deleteServe(Integer id) {
        Serve serve = this.getById(id);
        if (serve == null){
            throw new ForbiddenOperationException("区域服务不存在");
        }
        //判断是否选择区域
        if (serve.getRegionId() == null){
            throw new ForbiddenOperationException("未选择区域");
        }
        //判断是否是草稿，上下架都不允许删除
        if (serve.getSaleStatus() != FoundationStatusEnum.INIT.getStatus()){
            throw new ForbiddenOperationException("服务不是草稿");
        }
        boolean result = this.removeById(id);
        if (!result){
            throw new DBException("区域服务删除失败");
        }
    }


    /**
     * 区域设置热门
     * @param id
     */
    @Override
    public void onHot(Long id) {
        //判断服务是否存在
        Serve serve = this.getById(id);
        if (serve == null){
            throw new ForbiddenOperationException("区域服务不存在");
        }
        if (serve.getIsHot() == FoundationStatusEnum.ISHOT.getStatus()){
            throw new ForbiddenOperationException("区域服务已经是热门");
        }
        //判断是否选择区域
        if (serve.getRegionId() == null){
            throw new ForbiddenOperationException("未选择区域");
        }
        boolean result = this.lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getIsHot, FoundationStatusEnum.ISHOT.getStatus())
                .update();
        if (!result){
            throw new DBException("设置区域服务热门失败");
        }
    }


    /**
     * 区域服务取消热们
     * @param id
     */
    @Override
    public void offHot(Long id) {
        //判断服务是否存在
        Serve serve = this.getById(id);
        if (serve == null){
            throw new ForbiddenOperationException("区域服务不存在");
        }
        if (serve.getIsHot() == FoundationStatusEnum.ISNORTHOT.getStatus()){
            throw new ForbiddenOperationException("区域服务已经不是热门");
        }
        //判断是否选择区域
        if (serve.getRegionId() == null){
            throw new ForbiddenOperationException("未选择区域");
        }
        boolean result = this.lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getIsHot, FoundationStatusEnum.ISNORTHOT.getStatus())
                .update();
        if (!result){
            throw new DBException("设置区域服务取消热门失败");
        }
    }


}
